/**
 * 
 */
package com.gxljc.bear.crawler.itaogao.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ObjectUtils;
import org.apache.log4j.Logger;

import com.gxljc.bear.crawler.image.ImageService;
import com.gxljc.bear.crawler.util.HtmlUtil;

/**
 * @author haitao E-mail:haitao@summba.com
 * @version createTime：2021年9月19日 下午3:18:34
 * 
 */

public class ImageUtil {
	private final static Logger LOG = Logger.getLogger(ImageUtil.class);

	// logoUrl转换平台
	public static String imageConvertplatform(String imageUrl) throws Exception {
		String logoUrl = "";
		if (!imageUrl.isEmpty()) {
			ImageService imageService = ImageService.getInstance();
			Map<String, Object> imageMap = imageService.saveAndGetImage2UrlAndSize(imageUrl);
			if ("imageTypeOk".equals(ObjectUtils.toString(imageMap.get("status")))) {
				logoUrl = ObjectUtils.toString(imageMap.get("url"));
			}
		}
		return logoUrl;
	}

	public static List<String> imageConvertplatform(List<String> images) throws Exception {
		List<String> imagesPlatform = new ArrayList<String>();
		if (images.size() != 0) {
			for (String image : images) {
				ImageService imageService = ImageService.getInstance();
				Map<String, Object> imageMap = imageService.saveAndGetImage2UrlAndSize(image);
				if ("imageTypeOk".equals(ObjectUtils.toString(imageMap.get("status")))) {
					image = ObjectUtils.toString(imageMap.get("url"));
				}
				imagesPlatform.add(image);
			}
		}
		return imagesPlatform;
	}

	public static List<String> ImageUrlService(List<String> images, String htmlUrl) {
		List<String> imagesList = new ArrayList<String>();
		List<String> imagesPlatform = new ArrayList<String>();
		for (String image : images) {
			imagesList.add(imageUrlSupply(htmlUrl, image));
		}
		try {
			imagesPlatform = imageConvertplatform(imagesList);
		} catch (Exception e) {
			LOG.info("images convert Error！");
		}
		return imagesPlatform;
	}

	public static String imageUrlSupply(String htmlUrl, String imageUrl) {
		String imageUrlNews = "";
		if (!htmlUrl.isEmpty() && !imageUrl.isEmpty()) {
			imageUrlNews = HtmlUtil.pictureUrlPref(imageUrl, htmlUrl);
		}
		return imageUrlNews;
	}
}
